import { defineStore } from "pinia"
import defaultSettings from '@/settings'
import { useDynamicTitle } from '@/utils/dynamicTitle'

const { sideTheme, showSettings, topNav, tagsView, fixedHeader, sidebarLogo, dynamicTitle } = defaultSettings

const storageSetting = JSON.parse(localStorage.getItem('layout-setting')) || ''

export const settingStore = defineStore({
  id: 'settings', // 必填且唯一
  state: () => {
    return {
      title: '',
      theme: storageSetting.theme as string || '#409EFF',
      sideTheme: storageSetting.sideTheme || sideTheme,
      showSettings: showSettings,
      topNav: storageSetting.topNav === undefined ? topNav : storageSetting.topNav,
      tagsView: storageSetting.tagsView === undefined ? tagsView : storageSetting.tagsView,
      fixedHeader: storageSetting.fixedHeader === undefined ? fixedHeader : storageSetting.fixedHeader,
      sidebarLogo: storageSetting.sidebarLogo === undefined ? sidebarLogo : storageSetting.sidebarLogo,
      dynamicTitle: storageSetting.dynamicTitle === undefined ? dynamicTitle : storageSetting.dynamicTitle
    }
  },
  getters: {
    state: (state) => {
      return state
    }
  },
  actions: {
    // 修改布局设置
    changeSetting({ key, value }) {
      if (this.state.hasOwnProperty(key)) {
        this.state[key] = value
      }
    },
    // 设置网页标题
    setTitle(title: any) {
      this.title = title
      useDynamicTitle();
    }
  }
})